package com.lemon.myproject.common;

import com.alibaba.fastjson.JSONObject;
import com.lemon.myproject.exception.ExceptionEnum;
import com.lemon.myproject.service.BaseErrorInfoInterface;
import lombok.Getter;
import lombok.Setter;

/**
 * @Classname ResultResponse
 * @Description 返回响应结构体
 * @Date 2023/4/23 10:09
 * @Author PC
 */
@Setter
@Getter
public class ResultResponse {
    /**
     * 响应代码
     */
    private String code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应结果
     */
    private Object result;

    public ResultResponse() {
    }

    public ResultResponse(BaseErrorInfoInterface errorInfo) {
        this.code = errorInfo.getResultCode();
        this.message = errorInfo.getResultMsg();
    }

    /**
     * 成功
     *
     * @return {@link ResultResponse}
     * @Author PC
     * @Date 2023/4/23 10:53
     */
    public static ResultResponse success() {
        return success(null);
    }

    /**
     * 成功
     *
     * @param data
     * @return {@link ResultResponse}
     * @Author PC
     * @Date 2023/4/23 10:53
     */
    public static ResultResponse success(Object data) {
        ResultResponse rb = new ResultResponse();
        rb.setCode(ExceptionEnum.SUCCESS.getResultCode());
        rb.setMessage(ExceptionEnum.SUCCESS.getResultMsg());
        rb.setResult(data);
        return rb;
    }

    /**
     * 失败
     *
     * @param errorInfo
     * @return {@link ResultResponse}
     * @Author PC
     * @Date 2023/4/23 10:53
     */
    public static ResultResponse error(BaseErrorInfoInterface errorInfo) {
        ResultResponse rb = new ResultResponse();
        rb.setCode(errorInfo.getResultCode());
        rb.setMessage(errorInfo.getResultMsg());
        rb.setResult(null);
        return rb;
    }

    /**
     * 失败
     *
     * @param code
     * @param message
     * @return {@link ResultResponse}
     * @Author PC
     * @Date 2023/4/23 10:48
     */
    public static ResultResponse error(String code, String message) {
        ResultResponse rb = new ResultResponse();
        rb.setCode(code);
        rb.setMessage(message);
        rb.setResult(null);
        return rb;
    }

    /**
     * 失败
     *
     * @param message
     * @return {@link ResultResponse}
     * @Author PC
     * @Date 2023/4/23 10:21
     */
    public static ResultResponse error(String message) {
        ResultResponse rb = new ResultResponse();
        rb.setCode("-1");
        rb.setMessage(message);
        rb.setResult(null);
        return rb;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

}
